<?php

class Default_Models_Sale extends Libs_Model {

    public function __construct() {
        parent::__construct();
    }

    private $sale_id;
    private $pro_id;
    private $date_start;
    private $date_end;
    private $content;
    private $percent_decrease;

    public function setContent($content) {
        $this->content = $content;
    }

    public function getContent() {
        return $this->content;
    }

    public function setDateEnd($date_end) {
        $this->date_end = $date_end;
    }

    public function getDateEnd() {
        return $this->date_end;
    }

    public function setDateStart($date_start) {
        $this->date_start = $date_start;
    }

    public function getDateStart() {
        return $this->date_start;
    }

    public function setPercentDecrease($percent_decrease) {
        $this->percent_decrease = $percent_decrease;
    }

    public function getPercentDecrease() {
        return $this->percent_decrease;
    }

    public function setProId($pro_id) {
        $this->pro_id = $pro_id;
    }

    public function getProId() {
        return $this->pro_id;
    }

    public function setSaleId($sale_id) {
        $this->sale_id = $sale_id;
    }

    public function getSaleId() {
        return $this->sale_id;
        }

        public function setValue($row, $isKey = true) {
        $sale = new Default_Models_Sale();
        if ($isKey) {
            $sale->setSaleId($row['sale_id']);
        }
        $sale->setProId($row['pro_id']);
        $sale->setDateStart($row['date_start']);
        $sale->setDateEnd($row['date_end']);
        $sale->setContent($row['content']);
        $sale->setPercentDecrease($row['percent_decrease']);
        return $sale;
    }

    public function getKeyAndValue(Default_Models_Sale $sale, $isKey = false) {
        $data = array();
        if ($isKey) {
            $data['sale_id'] = $sale->getSaleId();
        }
        $data['pro_id'] = $sale->getProId();
        $data['date_start'] = $sale->getDateStart();
        $data['date_end'] = $sale->getDateEnd();
        $data['content'] = $sale->getContent();
        $data['percent_decrease'] = $sale->getPercentDecrease();
        return $data;
    }

    public function getSaleByProId($pro_id) {
        $sql = "select date_end from tbl_sales where pro_id = $pro_id order by date_end desc";
        $result = $this->db->fetchOne($sql);
        return $result['date_end'];
    }
    
    public function getPdByProId($pro_id) {
        $sql = "select percent_decrease from tbl_sales where pro_id = $pro_id order by date_end desc";
        $result = $this->db->fetchOne($sql);
        return $result['percent_decrease'];
    }
}
